package com.mvp.shq.washer.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mvp.shq.washer.R;
import com.mvp.shq.washer.entity.shop.ShopAttr;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/10/12.
 */

public class ShopAttrFlowAdapter extends RecyclerView.Adapter<ShopAttrFlowAdapter.ViewHolder> {

    private Context context;
    private List<ShopAttr> data;
    private OnAttrItemClickListener listener;


    public ShopAttrFlowAdapter(Context mContext, List<ShopAttr> list) {
        context = mContext;
        data = list;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_attr, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        ShopAttr shopAttr = data.get(position);
        holder.tvAttrName.setText(shopAttr.getAttrName());
        if (shopAttr.isSelect()){
            holder.imgSelect.setVisibility(View.VISIBLE);
            holder.rlContainer.setBackgroundResource(R.drawable.shop_spec_selected_bg);
        }else {
            holder.imgSelect.setVisibility(View.GONE);
            holder.rlContainer.setBackgroundResource(R.drawable.shop_spec_def_bg);
        }
        holder.rlContainer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onClick(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public void setListener(OnAttrItemClickListener listener) {
        this.listener = listener;
    }

    public interface OnAttrItemClickListener{
        void onClick(int position);
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_attr_name)
        TextView tvAttrName;
        @BindView(R.id.img_select)
        ImageView imgSelect;
        @BindView(R.id.rl_container)
        RelativeLayout rlContainer;
        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }
}
